package com.example.day_20.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day_20.DataBaseUtils.DbUtils;
import com.example.day_20.R;
import com.example.day_20.adapter.RlvCollectAdapter;
import com.example.day_20.bean.InFoBean;

import java.util.List;

public class CollectFragment extends Fragment {
    private RecyclerView mRlv;
    private RlvCollectAdapter rlvCollectAdapter;
    private boolean isCreadView = false;
    private boolean isLoadData = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collect, container, false);
        initView(view);
        isCreadView = true;
        initData();
        return view;
    }

    private void initData() {
        isLoadData = false;
        List<InFoBean.DataBean.DatasBean> select = DbUtils.select();
        rlvCollectAdapter.setDatas(select);
    }

    private void initView(@NonNull final View itemView) {
        mRlv = (RecyclerView) itemView.findViewById(R.id.rlv);

        mRlv.setLayoutManager(new LinearLayoutManager(getContext()));
        mRlv.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        rlvCollectAdapter = new RlvCollectAdapter(getContext());
        mRlv.setAdapter(rlvCollectAdapter);

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isCreadView && !isLoadData) {
            initData();
        }
    }
}
